//
//  BoardCharSprite.m
//  Archon
//
//  Created by Victor Penning on 28.05.09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "BoardCharSprite.h"


@implementation BoardCharSprite

//@synthesize cameraAngleVert,cameraAngleHoriz;
@synthesize cameraPos;
@synthesize afterRotateHotspot;

//static Vector3 defaultHotspot;
//static Vector3 defaultSize;
//static Vector3 defaultFrameSize;
//
//+(void)setDefaultFrameSize:(Vector3)size
//{
//	defaultFrameSize = size;
//}
//
//+(void)setDefaultHotspot:(Vector3)hotspot
//{
//	defaultHotspot = hotspot;
//}
//
//+(void)setDefaultSize:(Vector3)size
//{
//	defaultSize = size;
//}
//
//
//-(id) initWithFileName:(NSString*)filename FramesCount:(int)framesCount FramesPerLine:(int)framesPerLine
//{
//	//Vector3 frameSize = Vector3Make(64,64,0);
//	Vector3 frameSize = [Sprite defaultFrameSize];
//	
//	if(self=[super initWithFileNamed:filename])
//	{
//		float u,v;
//		float uWidth = frameSize.x/texture.width;
//		float vHeight = frameSize.y/texture.height;
//		
//		int row,col;
//
//		animsCount = 1;
//		animsArray = malloc(animsCount*sizeof(SpriteAnimDef));
//		
//		for(int i=0;i<animsCount;i++)
//		{
//			animsArray[i].framesCount = framesCount;
//			
//			// Alloc frames and copy it from animDefArray
//			animsArray[i].framesArray = malloc(animsArray[i].framesCount*sizeof(SpriteFrameDef));
//			for(int f=0;f<animsArray[i].framesCount;f++)
//			{
//				row = f/framesPerLine;
//				col = f%framesPerLine;
//				
//				u = col*frameSize.x/texture.width;
//				v = row*frameSize.y/texture.height;
//
//				//animsArray[i].framesArray[f].size = Vector3Make(32,32,0);
//				animsArray[i].framesArray[f].size = [Sprite defaultSize];
//				//animsArray[i].framesArray[f].hotspot = Vector3Make(16, 0, 32);
//				animsArray[i].framesArray[f].hotspot = [Sprite defaultHotspot];
//				animsArray[i].framesArray[f].textCoord[0] = Vector3Make(u, v, 0);
//				animsArray[i].framesArray[f].textCoord[1] = Vector3Make(u+uWidth, v+vHeight, 0);
//			}
//					
//			animsArray[i].frameFreq = 1.0f/60.0f;
//			animsArray[i].loop		= YES;
//			animsArray[i].reverse	= NO;
//		}
//		
//		[self setAnimation:0];
//		[self updateWithTime:0 DeltaTime:0];
//		
//		
//		for(int i=0;i<framesCount;i++)
//		{		
//			
//		}
//	}
//	
//	return self;
//}

-(int) renderSelfToBufferVertex:(Vector3*)vertexBuffer Color:(ColorRGBA*)colorBuffer TextCoord:(float*)textCoordBuffer
{
	if(hidden || blinkHide)
		return 0;
	
	Vector3 cameraOffset;
	vec3Sub(&pos,&cameraPos,&cameraOffset);
	
//	float a = atan2(1,1)*180/M_PI;
//	float b = atan2(-1,1)*180/M_PI;
//	float c = atan2(1,-1)*180/M_PI;
//	float d = atan2(-1,-1)*180/M_PI;
	
	float cameraAngleHoriz = M_PI/2 + atan2(cameraOffset.y, cameraOffset.x);
	
	//float cameraAngleHoriz = 6*(rand()%1000)/1000.f;
	
//	[self updateWithTime:0 DeltaTime:0];
	
	static Vector3 leftTopPos,rightTopPos,leftBottomPos,rightBottomPos;
	static Vector3 leftTopText,rightTopText,leftBottomText,rightBottomText;
	
	Vector3 hotspotedPos;
	Vector3 scaledSize = Vector3Make(size.x, 0, size.y);
	
	vec3Sub(&pos,&hotspot,&hotspotedPos);
	
	// Pos
	
	leftTopPos		=  Vector3Make(0,				0, 0);
	rightTopPos		=  Vector3Make(scaledSize.x,	0, 0);
	leftBottomPos	=  Vector3Make(0,				0, scaledSize.z);
	rightBottomPos	=  Vector3Make(scaledSize.x,	0, scaledSize.z);
	
	vec3Sub(&leftTopPos,    &hotspot,&leftTopPos);
	vec3Sub(&rightTopPos,   &hotspot,&rightTopPos);
	vec3Sub(&leftBottomPos, &hotspot,&leftBottomPos);
	vec3Sub(&rightBottomPos,&hotspot,&rightBottomPos);
	
	vec3MulFloat(&leftTopPos, scale, &leftTopPos);
	vec3MulFloat(&rightTopPos, scale, &rightTopPos);
	vec3MulFloat(&leftBottomPos, scale, &leftBottomPos);
	vec3MulFloat(&rightBottomPos, scale, &rightBottomPos);
	
	float tmp = 0;
	//if(pos.x==0 && pos.y==0)
	//NSLog(@"Camera angle=%i",cameraAngleHoriz*180/M_PI);
	//NSLog(@"Camera angle=%i",(int)(cameraAngleHoriz*180/M_PI));
	
	float posX = leftTopPos.x;
	leftTopPos.x = posX * cos(cameraAngleHoriz);
	leftTopPos.y = posX * sin(cameraAngleHoriz);

	posX = rightTopPos.x;
	rightTopPos.x = posX * cos(cameraAngleHoriz);
	rightTopPos.y = posX * sin(cameraAngleHoriz);

	posX = leftBottomPos.x;
	leftBottomPos.x = posX * cos(cameraAngleHoriz);
	leftBottomPos.y = posX * sin(cameraAngleHoriz);

	posX = rightBottomPos.x;
	rightBottomPos.x = posX * cos(cameraAngleHoriz);
	rightBottomPos.y = posX * sin(cameraAngleHoriz);

	vec3Add(&pos,&leftTopPos,&leftTopPos);
	vec3Add(&pos,&rightTopPos,&rightTopPos);
	vec3Add(&pos,&leftBottomPos,&leftBottomPos);
	vec3Add(&pos,&rightBottomPos,&rightBottomPos);
	
	vertexBuffer[0] = leftTopPos;
	vertexBuffer[1] = rightTopPos;
	vertexBuffer[2] = rightBottomPos;
	
	vertexBuffer[3] = leftTopPos;
	vertexBuffer[4] = rightBottomPos;
	vertexBuffer[5] = leftBottomPos;
	
	for(int i=0;i<6;i++)
	{
		vec3Sub(&(vertexBuffer[i]),&afterRotateHotspot,&(vertexBuffer[i]));
	}
	
	// Texture Coords 
	
	//leftTopText		= textCoord[0];
	leftTopText		= textCoordLeftTop;
	rightTopText	= Vector3Make(textCoordRightBottom.x, textCoordLeftTop.y, 0);
	leftBottomText	= Vector3Make(textCoordLeftTop.x, textCoordRightBottom.y, 0);;
	rightBottomText = textCoordRightBottom;
	
	textCoordBuffer[0] = leftTopText.x;
	textCoordBuffer[1] = leftTopText.y;
	
	textCoordBuffer[2] = rightTopText.x;
	textCoordBuffer[3] = rightTopText.y;
	
	textCoordBuffer[4] = rightBottomText.x;
	textCoordBuffer[5] = rightBottomText.y;
	
	textCoordBuffer[6] = leftTopText.x;
	textCoordBuffer[7] = leftTopText.y;
	
	textCoordBuffer[8] = rightBottomText.x;
	textCoordBuffer[9] = rightBottomText.y;
	
	textCoordBuffer[10] = leftBottomText.x;
	textCoordBuffer[11] = leftBottomText.y;
	
	
	// Vertex Colors
	for(int i=0;i<6;i++)
		colorBuffer[i] = ColorRGBAMake(1, 0, 1, 1);
	
	return 6;
}


@end
